package ca.eloas.judging.rest.repositories;

import ca.eloas.judging.model.JudgeObject;
import ca.eloas.judging.objectify.PersistenceService;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.Result;

import javax.inject.Inject;
import java.util.Iterator;
import java.util.Map;

/**
 * @author JP
 */
abstract public class AbstractOfyRepository<TYPE> implements Repository<TYPE> {

    @Inject
    PersistenceService<Objectify> persistence;
    private final Class<TYPE> type;

    public AbstractOfyRepository(Class<TYPE> type) {

        this.type = type;
    }

    protected Objectify getPersistence() {
        return persistence.manager();
    }

    public void delete(TYPE... c) {

        persistence.manager().delete().entities(c).now();
    }

    public void deleteAll(Iterable<TYPE> iter) {

        persistence.manager().delete().entities(iter).now();
    }

    public TYPE get(long id) {
        return persistence.manager().load().type(type).id(id).get();
    }

    public void save(TYPE... c) {

        persistence.manager().save().entities(c).now();
    }

    public Iterable<TYPE> listAll() {
        return persistence.manager().load().type(type).iterable();
    }

    public Iterable<TYPE> iterateAll() {
        return persistence.manager().load().type(type).iterable();
    }


}
